# 导包
import pymysql

connect = None
cursor = None
try:
    # 创建连接
    connect = pymysql.connect(host='localhost', port=3306, user='root', password='root', database='books',
                              autocommit=False)
    # 创建游标
    cursor = connect.cursor()
    # 执行sql
    sql = 'insert into t_hero (id, name, book_id) values (6, "孙悟空", "6");'
    cursor.execute(sql)
    print(cursor.rowcount)
    connect.commit()
except Exception as a:
    connect.rollback()
    print()
finally:
    # 关闭游标
    if cursor:
        cursor.close()
    # 关闭连接
    if connect:
        connect.close()
